<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字段名称" prop="fieldNameEn">
        <el-input
          v-model="queryParams.fieldNameEn"
          placeholder="请输入字段名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段中文名称" prop="fieldNameCn">
        <el-input
          v-model="queryParams.fieldNameCn"
          placeholder="请输入字段中文名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="fieldType">
        <el-select v-model="queryParams.fieldType" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.field_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="限制长度" prop="lengthLimit">
        <el-input
          v-model="queryParams.lengthLimit"
          placeholder="请输入限制长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="格式分类" prop="formatType">
        <el-select v-model="queryParams.formatType" placeholder="请选择格式分类" clearable>
          <el-option
            v-for="dict in dict.type.field_format"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="格式说明" prop="formatRemark">
        <el-input
          v-model="queryParams.formatRemark"
          placeholder="请输入格式说明"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传字段名称" prop="fieldNameUpload">
        <el-input
          v-model="queryParams.fieldNameUpload"
          placeholder="请输入上传字段名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示隐形" prop="displayType">
        <el-select v-model="queryParams.displayType" placeholder="请选择显示隐形" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认值" prop="deafultValue">
        <el-input
          v-model="queryParams.deafultValue"
          placeholder="请输入默认值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否分组字段" prop="groupByField">
        <el-input
          v-model="queryParams.groupByField"
          placeholder="请输入是否分组字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否主键" prop="keyField">
        <el-input
          v-model="queryParams.keyField"
          placeholder="请输入是否主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:interfaceField:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:interfaceField:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:interfaceField:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:interfaceField:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="interfaceFieldList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字段ID" align="center" prop="fieldId" />
      <el-table-column label="模型ID" align="center" prop="modelId" />
      <el-table-column label="字段名称" align="center" prop="fieldNameEn" />
      <el-table-column label="字段中文名称" align="center" prop="fieldNameCn" />
      <el-table-column label="类型" align="center" prop="fieldType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_type" :value="scope.row.fieldType"/>
        </template>
      </el-table-column>
      <el-table-column label="限制长度" align="center" prop="lengthLimit" />
      <el-table-column label="格式分类" align="center" prop="formatType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_format" :value="scope.row.formatType"/>
        </template>
      </el-table-column>
      <el-table-column label="格式说明" align="center" prop="formatRemark" />
      <el-table-column label="上传字段名称" align="center" prop="fieldNameUpload" />
      <el-table-column label="显示隐形" align="center" prop="displayType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.displayType"/>
        </template>
      </el-table-column>
      <el-table-column label="默认值" align="center" prop="deafultValue">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_default" :value="scope.row.deafultValue"/>
        </template>
      </el-table-column>
      <el-table-column label="是否分组字段" align="center" prop="groupByField">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.groupByField"/>
        </template>
      </el-table-column>
      <el-table-column label="是否主键" align="center" prop="keyField">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.keyField"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:interfaceField:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:interfaceField:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改字段管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字段名称" prop="fieldNameEn">
          <el-input v-model="form.fieldNameEn" placeholder="请输入字段名称" />
        </el-form-item>
        <el-form-item label="字段中文名称" prop="fieldNameCn">
          <el-input v-model="form.fieldNameCn" placeholder="请输入字段中文名称" />
        </el-form-item>
        <el-form-item label="类型" prop="fieldType">
          <el-select v-model="form.fieldType" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.field_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="限制长度" prop="lengthLimit">
          <el-input v-model="form.lengthLimit" placeholder="请输入限制长度" />
        </el-form-item>
        <el-form-item label="格式分类" prop="formatType">
          <el-select v-model="form.formatType" placeholder="请选择格式分类">
            <el-option
              v-for="dict in dict.type.field_format"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="格式说明" prop="formatRemark">
          <el-input v-model="form.formatRemark" placeholder="请输入格式说明" />
        </el-form-item>
        <el-form-item label="上传字段名称" prop="fieldNameUpload">
          <el-input v-model="form.fieldNameUpload" placeholder="请输入上传字段名称" />
        </el-form-item>
        <el-form-item label="显示隐形" prop="displayType">
          <el-select v-model="form.displayType" placeholder="请选择显示隐形">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认值" prop="deafultValue">
          <el-input v-model="form.deafultValue" placeholder="请输入默认值" />
        </el-form-item>
        <el-form-item label="是否分组字段" prop="groupByField">
          <el-input v-model="form.groupByField" placeholder="请输入是否分组字段" />
        </el-form-item>
        <el-form-item label="是否主键" prop="keyField">
          <el-input v-model="form.keyField" placeholder="请输入是否主键" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInterfaceField, getInterfaceField, delInterfaceField, addInterfaceField, updateInterfaceField } from "@/api/system/interfaceField";

export default {
  name: "InterfaceField",
  dicts: ['field_type', 'sys_yes_no', 'field_format'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字段管理表格数据
      interfaceFieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelId: null,
        fieldNameEn: null,
        fieldNameCn: null,
        fieldType: null,
        lengthLimit: null,
        formatType: null,
        formatRemark: null,
        fieldNameUpload: null,
        displayType: null,
        deafultValue: null,
        groupByField: null,
        keyField: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字段管理列表 */
    getList() {
      this.loading = true;
      listInterfaceField(this.queryParams).then(response => {
        this.interfaceFieldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fieldId: null,
        modelId: null,
        fieldNameEn: null,
        fieldNameCn: null,
        fieldType: null,
        lengthLimit: null,
        formatType: null,
        formatRemark: null,
        fieldNameUpload: null,
        displayType: null,
        deafultValue: null,
        groupByField: null,
        keyField: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fieldId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字段管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fieldId = row.fieldId || this.ids
      getInterfaceField(fieldId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字段管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fieldId != null) {
            updateInterfaceField(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInterfaceField(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fieldIds = row.fieldId || this.ids;
      this.$modal.confirm('是否确认删除字段管理编号为"' + fieldIds + '"的数据项？').then(function() {
        return delInterfaceField(fieldIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/interfaceField/export', {
        ...this.queryParams
      }, `interfaceField_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
